﻿'© 2004 - 2007 ActiveModules, Inc. All Rights Reserved
Imports System.ComponentModel
Imports System.Web.UI
Namespace DotNetNuke.Modules.ActiveForums.Controls
    <CLSCompliant(True), DefaultProperty("Text"), ToolboxData("<{0}:ImageButton runat=server></{0}:ImageButton>")> Public Class ImageButton
        Inherits System.Web.UI.WebControls.WebControl
        Implements IPostBackEventHandler

        Public Overridable Property Text() As String
            Get
                Dim o As Object = ViewState("Text")
                If o Is Nothing Then
                    Return String.Empty
                Else
                    Return CStr(o)
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("Text") = Value
            End Set
        End Property

        Dim _imageUrl As String
        Dim _navigateURL As String
        Dim _hSpace As Integer = 0
        Dim _vSpace As Integer = 0
        Dim _imageAlign As String = "absmiddle"
        Dim _PostBack As Boolean = True
        Dim _clientSideScript As String
        Dim _params As String
        Dim _Confirm As Boolean = False
        Dim _ConfirmMessage As String = ""
        Dim _EnableClientValidation As Boolean = False
        Dim _ValidationGroup As String = ""
        Dim _imageLocation As String = "LEFT"
        Dim _objectId As String = ""
        Private _PostBackScript As String


        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [ImageUrl]() As String
            Get
                Return _imageUrl
            End Get

            Set(ByVal Value As String)
                _imageUrl = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [NavigateUrl]() As String
            Get
                Return _navigateURL
            End Get

            Set(ByVal Value As String)
                _navigateURL = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("absmiddle")> Property [ImageAlign]() As String
            Get
                Return _imageAlign
            End Get

            Set(ByVal Value As String)
                _imageAlign = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [HSpace]() As String
            Get
                Return _hSpace.ToString
            End Get

            Set(ByVal Value As String)
                _hSpace = CInt(Value)
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [VSpace]() As String
            Get
                Return _vSpace.ToString
            End Get

            Set(ByVal Value As String)
                _vSpace = CInt(Value)
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [PostBack]() As Boolean
            Get
                Return _PostBack
            End Get

            Set(ByVal Value As Boolean)
                _PostBack = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [ClientSideScript]() As String
            Get
                Return _clientSideScript
            End Get

            Set(ByVal Value As String)
                _clientSideScript = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Params]() As String
            Get
                Return _params
            End Get

            Set(ByVal Value As String)
                _params = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Confirm]() As Boolean
            Get
                Return _Confirm
            End Get

            Set(ByVal Value As Boolean)
                _Confirm = Value
            End Set
        End Property

        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [ConfirmMessage]() As String
            Get
                Return _ConfirmMessage
            End Get

            Set(ByVal Value As String)
                _ConfirmMessage = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [EnableClientValidation]() As Boolean
            Get
                Return _EnableClientValidation
            End Get

            Set(ByVal Value As Boolean)
                _EnableClientValidation = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [ValidationGroup]() As String
            Get
                Return _ValidationGroup
            End Get

            Set(ByVal Value As String)
                _ValidationGroup = Value
            End Set
        End Property

        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [ImageLocation]() As String
            Get
                Return _imageLocation
            End Get

            Set(ByVal Value As String)
                _imageLocation = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> Property ObjectId() As String
            Get
                Return _objectId
            End Get

            Set(ByVal Value As String)
                _objectId = Value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue("")> ReadOnly Property PostBackScript() As String
            Get
                Return _PostBackScript
            End Get

        End Property

        ' Defines the Click event.
        '
        Public Event Click As EventHandler

        ' Invokes delegates registered with the Click event.
        '
        Protected Overridable Sub OnClick(ByVal e As EventArgs)
            RaiseEvent Click(Me, e)
        End Sub
        Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
            OnClick(New EventArgs)
        End Sub

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim sConfirm As String = ""
            Dim sOnClick As String = ""
            If Me.CssClass = "" Then
                Me.CssClass = "amtoolbaritem"
            End If
            Dim outerWriter As New HtmlTextWriter(writer)
            Dim sVoid As String = "javascript:void(0);"
            Dim sStatusOver As String = ""
            Dim sStatusOff As String = ""
            If [Confirm] Then
                sConfirm = "if (confirm('" & [ConfirmMessage] & "')){ [FUNCTIONS] };"
            End If
            sOnClick = [ClientSideScript]
            If Not Me.Attributes.Item("onclick") Is Nothing Then
                sOnClick &= Me.Attributes.Item("onclick")
            End If
            If Not sConfirm = "" Then
                sOnClick = sConfirm.Replace("[FUNCTIONS]", sOnClick)
            End If
            If EnableClientValidation Then
                sOnClick = "if (typeof(Page_ClientValidate) == 'function'){ if (Page_ClientValidate('" & ValidationGroup & "')){" & sOnClick & "};};"
            End If
            Dim sPostBack As String = Page.ClientScript.GetPostBackEventReference(Me, String.Empty)
            _PostBackScript = sPostBack
            If Enabled Then
                If [PostBack] = True Then
                    If Not sConfirm = "" Then
                        sPostBack = sConfirm.Replace("[FUNCTIONS]", sPostBack)
                    End If
                    If EnableClientValidation Then
                        sPostBack = "if (typeof(Page_ClientValidate) == 'function'){ if (Page_ClientValidate('" & ValidationGroup & "')){" & sPostBack & "};};"
                    End If
                    If Not [ClientSideScript] = "" Then
                        sPostBack = ClientSideScript & sPostBack
                    End If
                    outerWriter.AddAttribute(HtmlTextWriterAttribute.Href, "javascript:" & sPostBack)
                ElseIf Not [NavigateUrl] = "" Then
                    outerWriter.AddAttribute(HtmlTextWriterAttribute.Href, Page.ResolveUrl([NavigateUrl]))
                End If

                If Not sOnClick = "" Then
                    If Not ObjectId = "" Then
                        outerWriter.AddAttribute(HtmlTextWriterAttribute.Id, ObjectId)
                    End If
                    outerWriter.AddAttribute(HtmlTextWriterAttribute.Href, sVoid)
                    outerWriter.AddAttribute(HtmlTextWriterAttribute.Onclick, sOnClick)
                End If
                If Not NavigateUrl = String.Empty Then
                    outerWriter.RenderBeginTag(HtmlTextWriterTag.A)
                End If

            End If
            If (Not sOnClick = String.Empty Or Not sPostBack = String.Empty) And NavigateUrl = String.Empty Then
                If Not ObjectId = "" Then
                    writer.AddAttribute(HtmlTextWriterAttribute.Id, ObjectId)
                End If
                If [PostBack] And Enabled Then
                    writer.AddAttribute("onclick", sPostBack)
                ElseIf Not sOnClick = String.Empty And Enabled Then
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, sOnClick)
                End If
            End If
            If Not [Width].IsEmpty = True Then
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, [Width].ToString)
            End If

            If Not [Height].IsEmpty = True Then
                writer.AddStyleAttribute(HtmlTextWriterStyle.Height, [Height].ToString)
            ElseIf CssClass Is Nothing Then
                writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "22px")
            End If
            If Not [CssClass] Is Nothing Then
                writer.AddAttribute(HtmlTextWriterAttribute.Class, [CssClass].ToString)
            End If
            If Enabled Then
                writer.AddAttribute("onmouseover", "this.className='" & Me.CssClass & "_over';")
                writer.AddAttribute("onmouseout", "this.className='" & Me.CssClass & "';")
            End If

            writer.RenderBeginTag(HtmlTextWriterTag.Div)

            If ImageLocation.ToUpper = "LEFT" Then
                Dim innerWriter As New HtmlTextWriter(writer)
                If Not [ImageUrl] = "" Then
                    Dim imageWriter As New HtmlTextWriter(innerWriter)
                    imageWriter.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveUrl([ImageUrl]))
                    imageWriter.AddAttribute("hspace", [HSpace].ToString)
                    imageWriter.AddAttribute("vspace", [VSpace].ToString)
                    imageWriter.AddAttribute("border", "0")
                    'If [PostBack] Then

                    '    imageWriter.AddAttribute("onclick", sPostBack)
                    'End If
                    imageWriter.AddAttribute("align", [ImageAlign].ToString)
                    imageWriter.RenderBeginTag(HtmlTextWriterTag.Img)
                    imageWriter.RenderEndTag()
                End If

                innerWriter.Write("<span>" & [Text] & "</span>")
            ElseIf ImageLocation.ToUpper = "RIGHT" Then
                Dim innerWriter As New HtmlTextWriter(writer)
                innerWriter.Write("<span>" & [Text] & "</span>")
                If Not [ImageUrl] = "" Then
                    Dim imageWriter As New HtmlTextWriter(innerWriter)
                    imageWriter.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveUrl([ImageUrl]))
                    imageWriter.AddAttribute("hspace", [HSpace].ToString)
                    imageWriter.AddAttribute("vspace", [VSpace].ToString)
                    imageWriter.AddAttribute("border", "0")
                    'If [PostBack] Then
                    '    imageWriter.AddAttribute("onclick", sPostBack)
                    'End If
                    imageWriter.AddAttribute("align", [ImageAlign].ToString)
                    imageWriter.RenderBeginTag(HtmlTextWriterTag.Img)
                    imageWriter.RenderEndTag()
                End If
            ElseIf ImageLocation.ToUpper = "TOP" Then
                Dim innerWriter As New HtmlTextWriter(writer)
                If Not [ImageUrl] = "" Then
                    Dim imageWriter As New HtmlTextWriter(innerWriter)
                    imageWriter.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveUrl([ImageUrl]))
                    imageWriter.AddAttribute("hspace", [HSpace].ToString)
                    imageWriter.AddAttribute("vspace", [VSpace].ToString)
                    imageWriter.AddAttribute("border", "0")
                    'If [PostBack] Then
                    '    imageWriter.AddAttribute("onclick", sPostBack)
                    'End If
                    imageWriter.AddAttribute("align", [ImageAlign].ToString)
                    imageWriter.RenderBeginTag(HtmlTextWriterTag.Img)
                    imageWriter.RenderEndTag()
                End If
                innerWriter.Write("<br />")
                innerWriter.Write("<span>" & [Text] & "</span>")

            End If

            'innerWriter.RenderEndTag()
            writer.RenderEndTag()
            If Not NavigateUrl = String.Empty Then
                If Enabled Then
                    outerWriter.RenderEndTag()
                End If
            End If


        End Sub


    End Class
End Namespace